package com.example.livingadminmaster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.livingadminmaster.common.IdWorker;
import com.example.livingadminmaster.mapper.FacultyMapper;
import com.example.livingadminmaster.pojo.Faculty;
import com.example.livingadminmaster.service.FacultyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class FacultyServiceImpl extends ServiceImpl<FacultyMapper, Faculty> implements FacultyService {
    IdWorker idWorker = new IdWorker(1, 1);
    @Autowired
    private FacultyMapper facultyMapper;

    @Override
    public List<Faculty> getAllFaculties() {
        List<Faculty> faculties=facultyMapper.selectList(null);
        return faculties;
    }

    @Override
    public Faculty getFacultyId(long faculty_id) {
        return facultyMapper.selectById(faculty_id);
    }

    @Override
    public Faculty getFacultyUsername(String username) {
        return facultyMapper.selectOne(new LambdaQueryWrapper<Faculty>().eq(Faculty::getUsername, username));
    }

    @Override
    public List<Faculty> getFacultyGender(boolean gender) {
        return facultyMapper.selectList(new LambdaQueryWrapper<Faculty>().eq(Faculty::getGender, gender));
    }

    @Override
    public List<Faculty> getDepartment(String department) {
        return facultyMapper.selectList(new LambdaQueryWrapper<Faculty>().eq(Faculty::getDepartment, department));
    }

    @Override
    public List<Faculty> getRoomNumber(String room_number) {
        return facultyMapper.selectList(new LambdaQueryWrapper<Faculty>().eq(Faculty::getRoomNumber, room_number));
    }

    @Override
    public List<Faculty> getBuilding(String building) {
        return facultyMapper.selectList(new LambdaQueryWrapper<Faculty>().eq(Faculty::getBuilding, building));
    }

    @Override
    public List<Faculty> getStatus(boolean status) {
        return facultyMapper.selectList(new LambdaQueryWrapper<Faculty>().eq(Faculty::getStatus, status));
    }

    @Override
    public int addFaculty(Faculty faculty) {
        long faculty_id = idWorker.nextId();
        faculty.setFacultyId(faculty_id);
        return facultyMapper.insert(faculty);
    }

    @Override
    public boolean deleteFaculty(long faculty_id) {
        return this.removeById(faculty_id);
    }

    @Override
    public int updateFaculty(Faculty faculty) {
        return facultyMapper.updateById(faculty);
    }

    @Override
    public boolean updateStatus(long faculty_id, boolean status) {
        LambdaUpdateWrapper<Faculty> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Faculty::getFacultyId, faculty_id).set(Faculty::getStatus, status);
        return facultyMapper.update(null, updateWrapper) > 0;
    }


}
